package com.kevin.thread;

import java.util.concurrent.*;

public class MultiThreadTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        String res = "res";
        FutureTask<Integer> task01 = new FutureTask<>(new MyThread01(res));
        FutureTask<Integer> task02 = new FutureTask<>(new MyThread02(res));
        ExecutorService service = Executors.newWorkStealingPool(4);
        Future<String> submit = service.submit((Callable<String>) () -> {
            return "jdk1.8 ThreadPool";
        });
        System.out.println(submit.get());
        Thread thread01 = new Thread(task01);
        Thread thread02 = new Thread(task02);
        thread01.start();
        thread02.start();
        Integer integer01 = task01.get();
        Integer integer02 = task02.get();
        System.out.println(integer01+integer02);
    }
}

class MyThread01 implements Callable<Integer>{

    String res;

    public MyThread01(String res){
        this.res = res;
    }

    @Override
    public Integer call() throws Exception {
        int i=0;
        synchronized (res) {
            i = 0;
            Thread.sleep(3000);
            i += 10;
            res.notify();
        }
        return i;
    }
}

class MyThread02 implements Callable<Integer>{

    String res;

    public MyThread02(String res){
        this.res = res;
    }
    int i=0;
    @Override
    public Integer call() throws Exception {
        synchronized (res) {
            int i = 0;
            Thread.sleep(2000);
            res.wait();
        }
        return ++i;
    }
}
